import axios from 'axios'

export default {
  namespaced: true,
  state: {
    channels: [],
    currentChannel: ''
  },
  mutations: {
    updateChannels (state, payload) {
      state.channels = payload // 更新频道数据
    },
    updateCurrentChannel (state, payload) {
      state.currentChannel = payload // 更新激活频道
    }
  },
  actions: {
    //将context.commit解构为commit
    async  getChannels ({ commit }) {
      //将返回的数据解构，数据是res.data.data.cannels
      const { data: { data: { channels } } } = await axios.get('http://toutiao.itheima.net/v1_0/channels')
      commit('updateChannels', channels)
      //默认展开的是第一个频道
      commit('updateCurrentChannel', channels[0].id)
    }
  }
}
